import { IContext, IPanelTabPageControllerParams, IParam } from '@/core/interface';
import { PanelDetailController } from './panel-detail-controller';

/**
 * 面板分页面板控制器
 *
 * @export
 * @class PanelTabPageController
 * @extends {PanelDetailModel}
 */
export class PanelTabPageController extends PanelDetailController {
	/**
	 * 子项
	 *
	 * @type {string[]}
	 * @memberof PanelTabPageController
	 */
	public details: string[] = [];

	/**
	 * Creates an instance of PanelTabPageController.
	 * PanelTabPageController 实例
	 *
	 * @param {*} [IPanelTabPageControllerParams]
	 * @memberof PanelTabPageController
	 */
	constructor(opts: IPanelTabPageControllerParams, context: IContext, viewParams: IParam) {
		super(opts, context, viewParams);
		this.details = opts.details;
	}

	/**
	 * 加载数据
	 *
	 * @param {*} context
	 * @param {*} viewParams
	 * @memberof PanelTabPageController
	 */
	public async load(context: any, viewParams: any) {
		if (!this.parentName) {
			if (this.type == 'VIEWLAYOUT') {
				this.data = {};
			} else {
				this.data = this.panel.store.data;
			}
		} else {
			let parentItem =
				this.panel.store.layoutModelDetails[`${this.parentName}_${this.index}`];
			if (!parentItem) {
				parentItem = this.panel.store.layoutModelDetails[this.parentName];
			}
			if (parentItem) {
				if (
					parentItem.dataRegionType === 'MULTIDATA' &&
					parentItem['data'] &&
					parentItem['data'].length > 0
				) {
					this.data = parentItem.getData()[this.getIndex()];
				} else {
					this.data = parentItem.getData();
				}
			}
		}
	}

    /**
     * 获取标签动态样式表
     *
     * @protected
     * @return {*} 
     * @memberof PanelContainerController
     */
    protected getLabelDynaClass() {
        const dynaClass = {};
        try {
            const data = this.data;
            const content = this.context;
            const viewParams = this.viewParams;
            if (this.labelDynaClass) {
                Object.assign(dynaClass,...eval(this.labelDynaClass))
            }
        } catch (error) {
            
        }
        return dynaClass;
    }

    /**
     * 标签直接样式
     *
     * @memberof PanelContainerController
     */
    public getLabelStyle() {
        const labelStyle = {};
        if (this.labelCssStyle) {
            try {
                const labelStyles = this.labelCssStyle.split(';');
                if (labelStyles && labelStyles.length > 0) {
                    labelStyles.forEach((style: string) => {
                        if (style) {
                            const _style = style.split(':');
                            if (_style.length == 2) {
                                Object.assign(labelStyle, {
                                    [_style[0]]: _style[1].trim().substring(1, _style[1].length - 2)
                                })
                            }
                        }
                    })
                }
            } catch (error) {
                
            }
        }
        return labelStyle;
    }
}
